package com.example.teacherservice.controller;

import com.example.teacherservice.dto.ClassInfoDTO;
import com.example.teacherservice.dto.CourseInfoDTO;
import com.example.teacherservice.entity.Teacher;
import com.example.teacherservice.service.TeacherService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spoc/teacher")
public class TeacherController {
    private final TeacherService teacherService;

    public TeacherController(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    @GetMapping("/getTeacher/{id}")
    public ResponseEntity<Teacher> getTeacher(@PathVariable Long id) {
        Teacher teacher = teacherService.getTeacherById(id);
        return ResponseEntity.ok(teacher);
    }

    @GetMapping("/getTeacherByUid/{uid}")
    public ResponseEntity<Teacher> getTeacherByUid(@PathVariable Long uid) {
        Teacher teacher = teacherService.getTeacherByUserId(uid);
        return ResponseEntity.ok(teacher);
    }

    @GetMapping("/getUid/{id}")
    public ResponseEntity<Long> getUid(@PathVariable Long id) {
        Long userId = teacherService.getUserIdByTeacherId(id);
        return ResponseEntity.ok(userId);
    }

    @PostMapping("/updateTeacher")
    public ResponseEntity<Teacher> updateTeacher(@RequestBody Teacher teacher) {
        Teacher updatedTeacher = teacherService.updateTeacher(teacher);
        return ResponseEntity.ok(updatedTeacher);
    }

    // 新增接口：获取教师教授的班级
    @GetMapping("/{teacherId}/classes")
    public ResponseEntity<List<ClassInfoDTO>> getTeacherClasses(@PathVariable Long teacherId) {
        List<ClassInfoDTO> classes = teacherService.getTeacherClasses(teacherId);
        return ResponseEntity.ok(classes);
    }

    // 新增接口：获取教师教授的课程
    @GetMapping("/{teacherId}/courses")
    public ResponseEntity<List<CourseInfoDTO>> getTeacherCourses(@PathVariable Long teacherId) {
        List<CourseInfoDTO> courses = teacherService.getTeacherCourses(teacherId);
        return ResponseEntity.ok(courses);
    }

    // TeacherController.java
    @GetMapping("/teacher/{teacherId}/classes/{classId}")
    public ResponseEntity<Boolean> checkTeacherClass(
            @PathVariable Long teacherId,
            @PathVariable Long classId) {

        boolean hasPermission = teacherService.checkTeacherClassRelation(teacherId, classId);
        return ResponseEntity.ok(hasPermission);
    }
}